import { _decorator, Component, instantiate, Node, Prefab, UITransform, v3, Animation, Label, size } from 'cc';
import { BaseViewCmpt } from '../../Frame/Script/Base/baseViewCmpt';
import { App } from '../app';
import { ResLoadHelper } from '../../Frame/Script/resLoadHelper';
import { OverItem } from '../item/OverItem';
import { ViewName } from '../../Frame/Script/const/viewNameConst';
import CommonUtil from '../../Frame/Script/CommonUtil';
import { NextBlock } from '../item/NextBlock';
const { ccclass, property } = _decorator;
const BG_HEIGHT = [560, 660, 760];
const BTN_Y = [-120, -220, -300];

@ccclass('Model1OverViewManager')
export class Model1OverViewManager extends BaseViewCmpt {

    private panel_bg: Node = null;
    private item_par: Node = null;
    private btn_again: Node = null;
    private grid_par: Node = null;
    private block_area: Node = null;
    private score_label: Label = null;
    private remove_label: Label = null;

    private title_anim: Animation = null;

    protected onLoad(): void {
        super.onLoad();
        this.panel_bg = this.viewList.get("content/img_bg");
        this.item_par = this.viewList.get("content/itemPar");
        this.btn_again = this.viewList.get("content/btnAgain");
        this.grid_par = this.viewList.get("content/img_bg/img_rect/gridPar");
        this.block_area = this.viewList.get("content/img_bg/img_rect/blockArea");
        this.score_label = this.viewList.get("content/img_bg/scoreLabel").getComponent(Label);
        this.remove_label = this.viewList.get("content/img_bg/removeLabel").getComponent(Label);
        this.title_anim = this.viewList.get("content/img_bg/img_title_anim").getComponent(Animation);
    }

    start() {
        this.initPanel();
        this.initMap();
        this.showMap();
        this.playTitleAnim();
    }

    private async initPanel() {
        let seats = App.gameData.getGameSeats();
        let people_num = App.gameData.getPeopleNum();
        let idx = people_num - 2;
        idx = idx >= 0 ? idx : 0;
        let bg_height = BG_HEIGHT[idx];
        let btn_y = BTN_Y[idx];
        this.panel_bg.getComponent(UITransform).height = bg_height;
        this.btn_again.setPosition(v3(0, btn_y, 0));
        this.btn_again.active = App.user.seatId !== 0;
        this.score_label.string = '' + App.gameData.model1_totalScore;
        this.remove_label.string = '' + App.gameData.model1_clearLineCount;
        let pre: Prefab = await ResLoadHelper.loadAssetSync("prefabs/item/overItem", Prefab);
        let item: Node = null;
        for (let i = 0; i < 4; i++) {
            let seat_data = seats[i];
            let seatId = i + 1;
            if (seat_data.user) {
                let uid: number = seat_data.user.uid;
                let name: string = seat_data.user.name;
                let icon: string = seat_data.user.icon;
                let score: number = App.gameData.user_score_map[seatId];
                item = instantiate(pre);
                item.name = "p" + seatId;
                item.getComponent(OverItem).initItem(uid, name, icon, score);
                item.parent = this.item_par;
            }
        }
    }

    private initMap() {
        let tabular: number[] = App.gameData["TEAMWORk_" + App.gameData.getPeopleNum()];
        const [row, col] = tabular;
        let total = row * col;
        this.grid_par.getComponent(UITransform).width = col * 10;
        let rect = this.grid_par.children[0];
        for (let i = 0; i < total; i++) {
            if (i === 0) {
                rect.name = "rect_" + i;
            } else {
                let grid = instantiate(rect);
                grid.name = "rect_" + i;
                grid.parent = this.grid_par;
            }
        }
    }

    private async showMap() {
        let map = App.gameData.model1_map;
        console.log(map);

        let value: number = 0;
        let block: Node = null;
        let blockCom: NextBlock = null;
        let color_value: number = 0;
        let box_size = size(map[0].length * 10, map.length * 10);
        this.block_area.getComponent(UITransform).setContentSize(box_size);
        this.block_area.setPosition(v3(-box_size.width / 2, box_size.height / 2, 0));
        let block_pre = await ResLoadHelper.loadAssetSync("prefabs/item/nextBlock", Prefab) as Prefab;
        for (let row = 0, y = -10; row < map.length; row++, y -= 10) {
            for (let col = 0, x = 0; col < map[0].length; col++, x += 10) {
                if (!map[row + 3]) return;
                value = map[row + 3][col];
                color_value = (value / 10) | 0;
                block = instantiate(block_pre);
                block.getComponent(UITransform).setContentSize(size(9, 9));
                block.setPosition(v3(x, y, 0));
                blockCom = block.getComponent(NextBlock);
                blockCom.setBlock(color_value, value > 0);
                block.parent = this.block_area;
            }
        }
    }

    private playTitleAnim() {
        /** 需要根据语言自动匹配，更换为动态制作帧动画 */
        // CommonUtil.addSpriteAnimation();
        this.title_anim.play();
    }

    onClick_btnAgain() {
        this.onClick_closeBtn();
        App.view.openView(ViewName.Single.eHomeView);
    }

    onClick_btnClose() {
        this.onClick_closeBtn();
        App.view.openView(ViewName.Single.eHomeView);
    }

}


